package com.ruoyi.controller.masterdate;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.service.jpjs.param.ParamService;
import com.ruoyi.service.jpjs.person.PersonService;
import com.ruoyi.service.masterdate.CompanyService;
import com.ruoyi.service.masterdate.DepartmentService;
import com.ruoyi.service.masterdate.DepartmentService;
import com.ruoyi.service.masterdate.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/department")
public class DepartmentController extends BaseController {

    private String prefix = "masterdate/department";

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private EmployeeService employeeService;

    @GetMapping()
    public String person() {
        return prefix + "/Department";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo findDepartmentAll(@RequestParam Map<String, Object> map) {
        List<Map<String, Object>> list = departmentService.findDepartmentAll(map);
        return getDataTable(list);
    }

    @GetMapping("/add")
    public String addDepartment(ModelMap mmp) {
        mmp.put("parent",departmentService.findDepartmentAll(null));
        mmp.put("comp",companyService.findCompanyAll(null));
        mmp.put("employee",employeeService.findEmployeeAll(null));
        return prefix + "/add";
    }

    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addDepartment(@RequestParam Map<String, Object> map) {
        return toAjax(departmentService.addDepartment(map));
    }

    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editDepartment(@RequestParam Map<String, Object> map) {
        return toAjax(departmentService.editDepartment(map));
    }

    @GetMapping("/edit/{id}")
    public String editDepartment(@PathVariable("id") String id, ModelMap mmp) {
        mmp.put("parent",departmentService.findDepartmentAll(null));
        mmp.put("comp",companyService.findCompanyAll(null));
        mmp.put("employee",employeeService.findEmployeeAll(null));
        mmp.put("department", departmentService.findDepartment(id));
        return prefix + "/edit";
    }

    @PostMapping("/checkCodeUnique")
    @ResponseBody
    public int checkCodeUnique(@RequestParam Map<String, Object> map) {
        return departmentService.checkCodeUnique(map);
    }

}
